#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <memory.h>


void str_cut(char* ch, char* p[10])
{
    int i = 0;
    int j = 0;
    int k = 0;
    char tmp[10] = {0};
    while (ch[i] != '\n')
    {
        if (ch[i] != ',')
        {
            tmp[j] = ch[i];
            j++;
            i++;
        }
        else
        {
            char* ret = (char*)calloc(10, 1);
            strcpy(ret, tmp);
            p[k] = ret;
            memset(tmp, 0, 10);
            j = 0;
            i++;
            k++;
        }
    }
    if (ch[i] == '\n')
    {
        char* ret = (char*)malloc(10);
        strcpy(ret, tmp);
        p[k] = ret;
    }
}

void str_sort(char* p[10])
{
    int count = 0;
    while (p[count] != NULL)
    {
        count++;
    }
    int i = count - 1;
    int j = 0;

    for (i = count - 1; i > 0; i--)
    {
        for (; j < i; j++)
        {
            char tmp1[10] = {0};
            strcpy(tmp1, p[j]);
            char tmp2[10] = {0};
            strcpy(tmp2, p[j + 1]);
            if (strlen(tmp1) == 4)
            {
                memmove(&tmp1[1], tmp1, 4);
                tmp1[0] = '0';

            }
            if (strlen(tmp2) == 4)
            {
                memmove(&tmp2[1], tmp2, 4);
                tmp2[0] = '0';

            }
            if (strcmp(tmp1, tmp2) > 0)
            {
                char tmp[10] = {0};
                strcpy(tmp, p[j]);
                strcpy(p[j], p[j + 1]);
                strcpy(p[j + 1], tmp);
            }

        }
    }
}

int main()
{
    char ch[1024] = {0};
    printf("please input string :");

    fgets(ch, 1024, stdin);

    printf("ch : %s", ch);

    char* p[10];
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        p[i] = NULL;
    }

    str_cut(ch, p);
    i = 0;
    while (p[i] != NULL)
    {
        printf("p[%d] : %s\n", i, p[i++]);
    }

    str_sort(p);
    i = 0;
    while (p[i] != NULL)
    {
        printf("p[%d] : %s\n", i, p[i++]);
    }
    memset(ch, 0, 1024);
    i = 0;
    int sum = 0;
    while (p[i] != NULL)
    {
        memmove(ch + sum, p[i], strlen(p[i]));
        sum += strlen(p[i]);
        memmove(ch + sum, ",", 1);
        sum += 1;
        i++;
    }

    ch[strlen(ch) - 1] = '\0';

    printf("ch : %s\n", ch);

    return 0;
}
